﻿using Company.Database.Base.Entities;
using Company.Database.Base.Repositories;
using Company.WebServer.Models;
using Microsoft.AspNetCore.Mvc;

namespace Company.WebServer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HospitalController : ControllerBase
    {
        private readonly ILogger<WeatherForecastController> _logger;
        private IHospitalRepository HospitalRepository { get; }

        public HospitalController(
            ILogger<WeatherForecastController> logger,
            IHospitalRepository hospitalRepository)
        {
            HospitalRepository = hospitalRepository;
            _logger = logger;
        }


        /// <summary>
        /// 按关键字查询所有医院
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetHospitals")]
        public async Task<IActionResult> GetHospitals([FromQuery] string keyword)
        {
            Result<List<HospitalEntity>> result = new();
            var entities = await HospitalRepository.GetAllAsync();
            if(entities==null||!entities.Any())
            {
                result.Message = "没有数据";
                return new JsonResult(result);
            }
            entities = entities.FindAll(t => t.Name.Contains(keyword));
            if(entities.Any())
            {
                result.Success = true;
                result.Message = "操作成功";
                result.Value = entities;
            }
            return new JsonResult(result);
        }
    }
}
